//
//  MYGTopicTableViewCell.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/22.
//

import UIKit

class MYGTopicTableViewCell: UITableViewCell {
    @IBOutlet weak var likeCountBtn: UIButton!
    
    @IBOutlet weak var hospitalLB: UILabel!
    @IBOutlet weak var commentCountBtn: UIButton!
    @IBOutlet weak var shareCountBtn: UIButton!
    @IBOutlet weak var contentLB: UILabel!
    @IBOutlet weak var titleLB: UILabel!
    @IBOutlet weak var jobLB: UILabel!
    @IBOutlet weak var userNameLB: UILabel!
    @IBOutlet weak var avatar: UIImageView!
    var model = MYGTopicModel()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    @IBAction func tapShare(_ sender: UIButton) {
    }
    
    @IBAction func tapLike(_ sender: UIButton) {
        var likeStatus = "1"
        if self.model.isLike == 0 {
            likeStatus = "1"
            self.model.like = max(0, (self.model.like + 1))
            self.model.isLike = 1

        }else {
            likeStatus = "0"
            self.model.like = max(0, (self.model.like - 1))
            self.model.isLike = 0
        }
        updateLikeBtn()
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .likeSubmit(param: [
            "id":"\(self.model.id)",
           "like_type":"1",
            "like_status":likeStatus])) { item in
               
       } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
    
    @IBAction func tapComment(_ sender: UIButton) {
    }
    
    func bindData(_ data:MYGTopicModel) {
        self.model = data
        self.userNameLB.text = data.doctor.name
        self.avatar.sd_setImage(with: URL(string: data.doctor.avatar), placeholderImage: UIImage.init(named: "MYG_defaultAvatar_doctor"), context: nil)
        self.jobLB.text = data.doctor.title
        self.hospitalLB.text = data.doctor.hospital
        self.titleLB.text = data.title
        self.contentLB.text = data.desc
        self.shareCountBtn.setTitle(data.share_count, for: .normal)
        self.commentCountBtn.setTitle(data.comment, for: .normal)
        if data.comment.count == 0 {
            self.commentCountBtn.setTitle(data.comment_count, for: .normal)

        }
        updateLikeBtn()
    }
    
    func updateLikeBtn() {
        self.likeCountBtn.setTitle("\(self.model.like)", for: .normal)
        self.likeCountBtn.isSelected = self.model.isLike != 0
        self.likeCountBtn.setImage(self.model.isLike != 0 ? UIImage.init(named: "MYG_cloudDisk_liked") : UIImage.init(named: "MYG_cloudDisk_like_normal"), for: .selected)
    }
   
}
